package C;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class C57 {
    public static void main(String[] args) {
        Scanner sc  = new Scanner(System.in);
        int[] nums = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        System.out.println(getResult(nums));
    }

    public static int getResult(int[] nums) {
        int ans = Integer.MIN_VALUE;

        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(0);
        while(!stack.isEmpty()) {
            int father = stack.pop();
            int left = father*2+1;
            int right = father*2+2;
            //如果没有叶子结点，则父节点和答案比较
            if ((left>=nums.length || nums[left]==-1)
                    && (right>=nums.length || nums[left]==-1)) {
                ans = Math.max(nums[father],ans);
                continue;
            }
            //如果有叶子节点
            //如果有左叶子
            if (left<nums.length && nums[left]!=-1) {
                nums[left] += nums[father];
                stack.push(left);
            }
            //如果有右叶子
            if (right<nums.length && nums[left]!=-1) {
                nums[right] += nums[father];
                stack.push(right);
            }
        }

        return ans;
    }
}
